package headfirst.combined.djview;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.EventQueue;

import javax.swing.JFrame;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.KeyAdapter;

public class pad {

	
	private JFrame frame;
	private JButton botonP,botonB,botonR, botonManual;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					pad window = new pad();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public pad(){
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
	    
		botonManual = new JButton("Modo Pad");
		botonManual.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent tecla) {
				if(tecla.VK_S==tecla.getKeyCode()){
					botonB.setBackground(Color.RED);
					playBombo();
				}
				if(tecla.VK_A==tecla.getKeyCode()){
					botonP.setBackground(Color.BLUE);
					playPlatillo();
				}
				if(tecla.VK_D==tecla.getKeyCode()){
					botonR.setBackground(Color.GREEN);
					playRedoblante();
				}
			}
			@Override
			public void keyReleased(KeyEvent tecla) {
				if(tecla.VK_S==tecla.getKeyCode()){
					botonB.setBackground(null);
				}
				if(tecla.VK_A==tecla.getKeyCode()){
					botonP.setBackground(null);
				}
				if(tecla.VK_D==tecla.getKeyCode()){
					botonR.setBackground(null);
				}
			}
		});
		botonManual.setBounds(170, 1, 120, 54);
		frame.getContentPane().add(botonManual);
		
		botonB = new JButton("Bombo");
		botonB.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				playBombo();
			}
		});
		botonB.setBounds(179, 140, 89, 54);
		frame.getContentPane().add(botonB);
		
		
		
		
		botonP = new JButton("Platillo");
		botonP.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				playPlatillo();
			}
		});
		botonP.setBounds(55, 140, 89, 54);
		frame.getContentPane().add(botonP);
		
		
		botonR = new JButton("Redoblante");
		botonR.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				playRedoblante();
			}
		});
		botonR.setBounds(303, 140, 89, 54);
		frame.getContentPane().add(botonR);	
	
	
	
	} // end metodo
	
	public void playBombo(){
		try {
			Clip sonido = AudioSystem.getClip();
			File a = new File("sonidos\\wavB.wav");
			sonido.open(AudioSystem.getAudioInputStream(a));
			sonido.start();
			System.out.println("Dum");
			//Thread.sleep(2000); // 1000 milisegundos (1 segundos)
			//sonido.close();
			}
			catch (Exception tipoerror) {
			System.out.println("" + tipoerror);
			}
	}
	
	public void playPlatillo(){
		try {
			Clip sonido = AudioSystem.getClip();
			File a = new File("sonidos\\wavP.wav");
			sonido.open(AudioSystem.getAudioInputStream(a));
			sonido.start();
			System.out.println("Tss");
			//Thread.sleep(1200); // 1000 milisegundos (1 segundos)
			//sonido.close();
			}
			catch (Exception tipoerror) {
			System.out.println("" + tipoerror);
			}
	}
	
	public void playRedoblante(){
		try {
			Clip sonido = AudioSystem.getClip();
			File a = new File("sonidos\\wavR.wav");
			sonido.open(AudioSystem.getAudioInputStream(a));
			sonido.start();
			System.out.println("Tash");
			//Thread.sleep(2000); // 1000 milisegundos (1 segundos)
			//sonido.close();
			}
			catch (Exception tipoerror) {
			System.out.println("" + tipoerror);
			}
	}
}
